package libs

import (
	"github.com/apache/rocketmq-clients/golang/v5"
	"github.com/apache/rocketmq-clients/golang/v5/credentials"
	"os"
	"time"
)

const (
	// 事物消息
	// ./mqadmin updateSubGroup -n localhost:9876 -c DefaultCluster -g transaction_group
	TransactionGroup = "transaction_group"
	// ./mqadmin updatetopic -n localhost:9876 -t transaction -c DefaultCluster -a +message.type=TRANSACTION
	TransactionTopic = "transaction"

	// maximum waiting time for receive func
	awaitDuration = time.Second * 5
	// maximum number of messages received at one time
	//maxMessageNum int32 = 16
	// invisibleDuration should > 20s
	//invisibleDuration = time.Second * 20
)

func NewSimpleConsumer(Topic string, ConsumerGroup string) (golang.SimpleConsumer, error) {
	os.Setenv("mq.consoleAppender.enabled", "true")
	golang.ResetLogger()
	// In most case, you don't need to create many consumers, singleton pattern is more recommended.
	return golang.NewSimpleConsumer(
		&golang.Config{
			Endpoint:      RocketmqConfig.Endpoint,
			ConsumerGroup: ConsumerGroup,
			Credentials: &credentials.SessionCredentials{
				AccessKey:    RocketmqConfig.AccessKey,
				AccessSecret: RocketmqConfig.AccessSecret,
			},
		},
		golang.WithAwaitDuration(awaitDuration),
		golang.WithSubscriptionExpressions(map[string]*golang.FilterExpression{
			Topic: golang.SUB_ALL,
		}),
	)
}
